package v1

import (
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// AddArticle @Summary 添加文章
// @Description 添加文章并返回文章信息
// @Tags Article
// @Accept json
// @Produce json
// @Param status body int true "状态码"
// @Param message body string true "消息"
//
// @Success 200
// @Router /api/v1/article [post]
func AddArticle(c *gin.Context) {
	var data model.Article
	_ = c.ShouldBindJSON(&data)
	code = model.CreateArticle(&data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrorMsg(code),
	})

}

// GetCateArticle @Summary 查询分类下的所有文章
// @Description 查询指定分类下的文章列表，支持分页
// @Tags Article
// @Accept json
// @Produce json
// @Param id path int true "分类ID"
// @Param pagesize query int false "每页数量"
// @Param pagenum query int false "页码"
// @Router /api/v1/category/{id}/articles [get]
func GetCateArticle(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	//if pageSize == 0 {
	//	pageSize = -1
	//}
	if pageNum == 0 {
		pageNum = -1
	}
	id, _ := strconv.Atoi(c.Param("id"))
	data, code, total := model.GetCateArticle(id, pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrorMsg(code),
	})
}

// GetArtInfo @Summary 查询单个文章
// @Description 查询指定文章的详细信息
// @Tags Article
// @Accept json
// @Produce json
// @Param id path int true "文章ID"
// @Router /api/v1/article/{id} [get]
func GetArtInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	data, code := model.GetArtInfo(id)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrorMsg(code),
	})
}

// GetArticleList @Summary 查询文章列表
// @Description 查询文章列表，支持分页和筛选条件
// @Tags Article
// @Accept json
// @Produce json
// @Param title query string false "文章标题关键词"
// @Param pagesize query int false "每页数量"
// @Param pagenum query int false "页码"
//
// @Router /api/v1/article/list [get]
func GetArticleList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	title := c.Query("title")
	if pageNum == 0 {
		pageNum = -1
	}
	data, code, total := model.GetArtList(title, pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrorMsg(code),
	})
}

// UpdateArticle @Summary 编辑文章
// @Description 编辑指定文章信息
// @Tags Article
// @Accept json
// @Produce json
// @Param id path int true "文章ID"
// @Router /api/v1/article/{id} [put]
func UpdateArticle(c *gin.Context) {
	var data model.Article
	c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.UpdateArticle(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrorMsg(code),
	})
}

// DeleteArticle @Summary 删除文章
// @Description 根据文章ID删除文章
// @Tags Article
// @Accept json
// @Produce json
// @Param id path int true "文章ID"
// @Router /api/v1/article/{id} [delete]
func DeleteArticle(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteArticle(id)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrorMsg(code),
	})
}
